﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Outlook;

namespace OutlookComponent
{
    public class MeetingRoom
    {
        public MeetingRoom() { }

        public MeetingRoom(AddressEntry meetingRoomAddress)
        {
            Address = meetingRoomAddress;
            Name = meetingRoomAddress.Name.ToString();
            ExtractRoomInfo(meetingRoomAddress.Name);
        }

        public AddressEntry Address { get; set; }
        public int Capacity { get; set; }
        public Location RoomLocation { get; set; }
        public bool HasProjector { get; set; }
        public string Name { get; set; }

        private void ExtractRoomInfo(string meetingRoomName)
        {
            // Get location information
            string locationString = meetingRoomName.Substring(meetingRoomName.IndexOf('(') + 1, meetingRoomName.IndexOf(')') - meetingRoomName.IndexOf('(') - 1).ToLower();
            RoomLocation = new Location();
            if (locationString.Contains("arca"))
            {
                RoomLocation.Building = Building.ARCA;
                RoomLocation.Floor = Convert.ToInt32(locationString.Substring(locationString.IndexOf('-') + 1, 1));
                string regionString = locationString.Substring(locationString.IndexOf(',') + 1).Trim();
                switch (regionString)
                {
                    case "e":
                    case "ez":
                        RoomLocation.Region = Direction.EAST;
                        break;
                    case "n":
                    case "nz":
                        RoomLocation.Region = Direction.NORTH;
                        break;
                    case "w":
                    case "wz":
                        RoomLocation.Region = Direction.WEST;
                        break;
                    case "s":
                    case "sz":
                        RoomLocation.Region = Direction.SOUTH;
                        break;
                    case "ne":
                        RoomLocation.Region = Direction.NORTHEAST;
                        break;
                    case "se":
                        RoomLocation.Region = Direction.SOUTHEAST;
                        break;
                    case "nw":
                        RoomLocation.Region = Direction.NORTHWEST;
                        break;
                    case "sw":
                        RoomLocation.Region = Direction.SOUTHWEST;
                        break;
                }
            }
            else
                if (locationString.Contains("info"))
                {
                    RoomLocation.Building = Building.INFO;
                    RoomLocation.Floor = Convert.ToInt32(locationString.Substring(locationString.IndexOf('-') + 1, 1));

                }
                else
                    RoomLocation.Building = Building.Others;

            // Get capacity number
            if (meetingRoomName.IndexOf('[') >= 0 & meetingRoomName.IndexOf(']') >= 0)
            {
                string capacityNum = meetingRoomName.Substring(meetingRoomName.IndexOf('['), meetingRoomName.IndexOf(']') - meetingRoomName.IndexOf('[') + 1);
                Capacity = Convert.ToInt32(capacityNum.Trim('[', ']').Trim());
            }
            else
            {
                Capacity = 4;
            }

            // Whether having projector
            HasProjector = false;
            foreach (string projectorFlag in meetingRoomName.Split(','))
            {
                if (projectorFlag.Trim().ToLower() == "p")
                {
                    HasProjector = true;
                    break;
                }
            }
        }

        public override string ToString()
        {
            return string.Format("{0}, Capacity:{1}, {2}, Has Projector: {3}", this.Address.Name, this.Capacity, this.RoomLocation, this.HasProjector);
        }

        public class Location
        {
            public Building Building { get; set; }
            public int Floor { get; set; }
            public Direction Region { get; set; }

            public override string ToString()
            {
                return string.Format("Building: {0}, Floor: {1}, Area: {2}", Building, Floor, Region);
            }
        }

        public enum Building
        {
            INFO, ARCA, Others
        }

        public enum Direction
        {
            EAST = 0x0001, NORTH = 0x0010, WEST = 0x0100, SOUTH = 0x1000,
            NORTHEAST = EAST | NORTH, NORTHWEST = NORTH | WEST,
            SOUTHEAST = SOUTH | EAST, SOUTHWEST = SOUTH | WEST,
            UNKOWN = 0
        }

       
    }

    public static class RegionHelper
    {
        public static double Compare(MeetingRoom.Direction d1, MeetingRoom.Direction d2)
        {
            if (d1 == MeetingRoom.Direction.UNKOWN | d2 == MeetingRoom.Direction.UNKOWN)
                return 1;
            string a = string.Format("{0:x}", d1 & d2);
            string b = string.Format("{0:x}", d1 | d2);
            if (GetNumber(b) == 0)
            {
                return 0;
            }
            return GetNumber(a) / GetNumber(b);
        }

        private static double GetNumber(string number)
        {
            int i = 0;
            foreach (char numChar in number)
            {
                if (numChar == '1')
                {
                    i++;
                }
            }
            return Convert.ToDouble(i);
        }
    }
}
